package com.google.coffeeshop.gui.desktop.employee;

import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import com.google.coffeeshop.business.ManagerBusiness;
import com.google.coffeeshop.entity.EmployeeDTO;

public class EmployeeActionsPanel extends JPanel {
	private EmployeePanel employeePanel;
	
	public EmployeeActionsPanel(final EmployeePanel panel) {
		this.employeePanel = panel;
		JToolBar toolbar = new JToolBar();
		
		JButton btnAdd = new JButton("Add");
		btnAdd.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnAdd.setHorizontalTextPosition(SwingConstants.CENTER);
		btnAdd.setIcon(new ImageIcon(EmployeeActionsPanel.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/add-24.png")));
		btnAdd.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				AddEditEmployeeFrame frmAddEditEmployee = new AddEditEmployeeFrame(employeePanel, null);
				frmAddEditEmployee.setVisible(Boolean.TRUE);
			}
		});
		setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		toolbar.add(btnAdd);
		
		JButton btnEdit = new JButton("Edit");
		btnEdit.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnEdit.setHorizontalTextPosition(SwingConstants.CENTER);
		btnEdit.setIcon(new ImageIcon(EmployeeActionsPanel.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/edit-24.png")));
		btnEdit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				EmployeeDTO employee = employeePanel.getEmployeeSelected();
				if(employee != null){
					AddEditEmployeeFrame frmAddEditEmployee = new AddEditEmployeeFrame(employeePanel, employee);
					frmAddEditEmployee.setVisible(Boolean.TRUE);
				}
			}
		});
		
		toolbar.add(btnEdit);
		JButton btnLeave = new JButton("Leave Job");
		btnLeave.setVerticalTextPosition(SwingConstants.BOTTOM);
		btnLeave.setHorizontalTextPosition(SwingConstants.CENTER);
		btnLeave.setIcon(new ImageIcon(EmployeeActionsPanel.class.getResource("/com/google/coffeeshop/gui/desktop/icon/size24/leave-job-24.png")));
		btnLeave.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				EmployeeDTO employee = employeePanel.getEmployeeSelected();
				if (employee != null) {
					ManagerBusiness.leaveEmployee(employee.getId());
					employeePanel.refreshGUI();
					
				}
			}
		});
		toolbar.add(btnLeave);
		
		add(toolbar);
	}

	private static final long serialVersionUID = 1L;

}
